﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Core.Util
{
    public class StreamHelper
    {
        /// <summary>
        /// 转为内存流
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MemoryStream ToMemoryStream(Stream stream)
        {
            if ((stream as MemoryStream) != null)
            {
                return stream as MemoryStream;
            }
            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, b.Length);
            MemoryStream ms = new MemoryStream();
            ms.Write(b, 0, b.Length);
            return ms;
        }

        /// <summary>
        /// 转为byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] ToByte(Stream stream)
        {
            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, b.Length);
            return b;
        }

        /// <summary>
        /// 转为Stream
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static Stream ByteToStream(byte[] bytes)
        {
            var ms = new MemoryStream(bytes);
            ms.Position = 0;
            return ms;
        }
    }
}
